package com.example.nfc1;

import android.annotation.SuppressLint;
import android.nfc.NdefRecord;

import androidx.core.util.Preconditions;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;

public class MyNFCRecordParse {
    @SuppressLint("RestrictedApi")
    public static void parseWellKnownUriRecord(NdefRecord record) {
        Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_URI));
        byte[] payload = record.getPayload();
        String prefix = "https://www.baidu.com"; // 修改前缀为完整的 URI
//        byte[] prefixBytes = prefix.getBytes(StandardCharsets.UTF_8);
//        byte[] fullUri = new byte[prefixBytes.length + payload.length - 1];
//        System.arraycopy(prefixBytes, 0, fullUri, 0, prefixBytes.length);
//        System.arraycopy(payload, 1, fullUri, prefixBytes.length, payload.length - 1);

        // 在这里使用 fullUri 进行后续操作
    }

    public static void  parseAbsoulteUriRecord(){

    }
    @SuppressLint("RestrictedApi")
    public static void  parseWellKnownTextRecord(NdefRecord record){
        Preconditions.checkArgument(Arrays.equals(record.getType(),NdefRecord.RTD_URI));
        byte[] paylod  = record.getPayload();
        Byte statusByte = record.getPayload()[0];
        String textEncoding = ((statusByte & 0x80) == 0) ? "UTF-8" : "UTF-16";
        int langLength = statusByte & 0x3F;
        String langCode = new String(paylod,1,langLength, Charset.forName("UTF-8"));
        try {
            String payLoadText = new String(paylod,langLength+1,paylod.length-langLength-1,textEncoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    public static void  parseMimeRecord(){

    }

    public static void  parseExternalRecord(){

    }


}
